const mongoose = require('mongoose');
const reviewSchema = new mongoose.Schema({
    review: {
        type: String,
        required: [true, '评论不能为空']
    },
    rating: {
        type: Number,
        min: 1,
        max: 5,
    },
    createdAt: {
        type: Date,
        default: Date.now
    },
    tour: {
        type: mongoose.Schema.ObjectId,
        ref: 'Tour',
        required: [true, '评论必须属于旅途']
    },
    user: {
        type: mongoose.Schema.ObjectId,
        ref: 'User',
        required: [true, '评论必须属于一个用户']
    }
}, {
    toJSON: {
        virtuals: true
    },
    toObject: {
        virtuals: true
    }
})
// 联表查询既视感
reviewSchema.pre(/^find/,function (next) {
    // this.populate({
    //     path: 'tour',
    //     select: 'name'
    // }).populate({
    //     path: 'user',
    //     select: 'name photo'
    // })
    this.populate({
        path: 'user',
        select: 'name photo'
    })
    next()
})
const Review = mongoose.model('Review', reviewSchema);
module.exports = Review